package com.vgmobile.cucrever2.product.list;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.vgmobile.cucrever2.R;
import com.vgmobile.cucrever2.api.ItemProductList;
import com.vgmobile.cucrever2.constant.KeyFragment;
import com.vgmobile.cucrever2.global.BaseActivity;
import com.vgmobile.cucrever2.product.detail.ProductDetailActivity;

public class ProductListActivity extends BaseActivity {

	String cateId;
	String cateName;
	String cateImgLink;
	
	private ImageView imgBannerTop;
	private ListProductAdapter productListAdapter;
	private GridProductAdapter productGridAdapter;
	private ListView listView;
	private GridView gridView;
	private TextView txtTitle;
	private ArrayList<ItemProductList> listItem;
	private View btnFilter, btnArrange, btnView, btnViewList, btnViewGrid;
	
	private ImageLoader imageLoader;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.product_list_activity);
		
		Bundle params = getIntent().getExtras();
		cateId = params.getString(KeyFragment.KEY_CATE_ID);
		cateName= params.getString(KeyFragment.KEY_CATE_NAME);
		cateImgLink = params.getString(KeyFragment.KEY_CATE_IMG_LINK);
		
		imgBannerTop =(ImageView) findViewById(R.id.img_banner_top);
		imageLoader = ImageLoader.getInstance();
		imageLoader.displayImage(cateImgLink, imgBannerTop);
		
		btnFilter = findViewById(R.id.product_list_barbottom_btn_filter);
		btnArrange = findViewById(R.id.product_list_barbottom_btn_arrange);
		btnView = findViewById(R.id.product_list_barbottom_btn_view);
		btnViewList = findViewById(R.id.product_list_barbottom_btn_view_list);
		btnViewGrid = findViewById(R.id.product_list_barbottom_btn_view_grid);
		txtTitle = (TextView) findViewById(R.id.product_list_bar_texttitle);
		listView = (ListView) findViewById(R.id.product_list_listview);
		gridView = (GridView) findViewById(R.id.product_list_gridview);
		
		txtTitle.setText(cateName);
		listItem = new ArrayList<ItemProductList>();
		for (int i = 0; i < 15; i++) {
			ItemProductList itemProductList = new ItemProductList();
			itemProductList.setTitle("Sản phẩm thứ " + i);
			itemProductList.setStore("Cửa hàng ABC");
			itemProductList.setLocation("Vận chuyển toàn quốc");
			itemProductList.setPrice("20.036.123 đ");
			itemProductList.setImage("http://www.bongda.com.vn/Data/Image/2014/Thang05/11/Dung%203.jpg");
			itemProductList.setRating(i%9);
			listItem.add(itemProductList);
		}
		productListAdapter = new ListProductAdapter(this, listItem);
		productGridAdapter = new GridProductAdapter(this, listItem);
		listView.setAdapter(productListAdapter);
		gridView.setAdapter(productGridAdapter);
		
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> adapterView, View view, int postion, long id) {
				Intent intent = new Intent(activity, ProductDetailActivity.class);
				activity.startActivity(intent);
		        activity.overridePendingTransition(R.anim.activity_fade_in, R.anim.activity_fade_out);
			}
		});
	}
	
	public void btnView(View view){
		if(gridView.getVisibility() == View.GONE){
			btnViewList.setVisibility(View.GONE);
			btnViewGrid.setVisibility(View.VISIBLE);
			listView.setVisibility(View.GONE);
			gridView.setVisibility(View.VISIBLE);
		}else{
			btnViewList.setVisibility(View.VISIBLE);
			btnViewGrid.setVisibility(View.GONE);
			listView.setVisibility(View.VISIBLE);
			gridView.setVisibility(View.GONE);
		}
	}
}
